package com.hh.workorder.service;

import com.hh.workorder.entity.RepairWorkOrder;
import com.hh.workorder.repository.RepairWorkOrderRepository;
import com.hh.workorder.repository.RepairWorkOrderSpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RepairWorkOrderService {

    private final RepairWorkOrderRepository repository;

    @Autowired
    public RepairWorkOrderService(RepairWorkOrderRepository repository) {
        this.repository = repository;
    }

    public List<RepairWorkOrder> getAllRepairWorkOrders() {
        return repository.findAll();
    }

    public List<RepairWorkOrder> searchRepairWorkOrders(String id_like, String title_like, String status, String device_like, String assignee, String startDate, String endDate) {
        try {
            Specification<RepairWorkOrder> spec = new RepairWorkOrderSpecification(id_like, title_like, status, device_like, assignee, startDate, endDate);
            return repository.findAll(spec);
        } catch (Exception e) {
            // 这里可以记录日志，根据实际情况处理异常
            e.printStackTrace();
            return List.of();
        }
    }
    public RepairWorkOrder save(RepairWorkOrder workOrder) {
        return repository.save(workOrder);
    }
}